//
//  SMPersonalEnumView.swift
//  SomePasti
//
//  Created by 李永彬 on 2025/10/14.
//

import SwiftUI

struct SMPersonalEnumView: View {
    let dataSource: [Phasbook]
    var onSelectionChanged: ((Int, Phasbook?) -> Void)?
    var onConfirmAction: ((Phasbook?) -> Void)?
    
    @State private var selectedID: UUID?
    
    var body: some View {
        VStack(spacing: 0) {
            
            Spacer()
                .frame(height: 25)
            titleView
            
            ScrollView {
                LazyVStack(spacing: 10) {
                    ForEach(dataSource) { item in
                        rowView(for: item)
                    }
                }
            }
            .padding(.top, 35)
            
            Spacer()
            
            confirmButton
        }
    }
    
    private var titleView: some View {
        Text("Date Selection")
            .font(.system(size: 18, weight: .medium))
            .frame(maxWidth: .infinity, alignment: .center)
            .padding(.bottom, 16)
    }
    
    private func rowView(for item: Phasbook) -> some View {
        HStack {
            Spacer()
            Text(item.sarcling ?? "未命名")
                .font(.system(size: 14))
                .foregroundColor(item.id == selectedID ? .white : .black)
                .multilineTextAlignment(.leading)
                .padding(.horizontal, 10)
                .padding(.vertical, 5)
                .background(
                    Capsule() // 使用 Capsule 实现半圆效果
                        .fill(item.id == selectedID ? Color.blue : Color.clear)
                )
            
            Spacer()
        }
        .padding(.horizontal, 16) // row 的水平 padding
        .contentShape(Rectangle())
        .onTapGesture {
            selectedID = item.id
            let index = dataSource.firstIndex { $0.id == selectedID } ?? -1
            onSelectionChanged?(index, item)
        }
    }
    
    private var confirmButton: some View {
        Button(action: {
            let selectedItem = dataSource.first { $0.id == selectedID }
            onConfirmAction?(selectedItem)
        }) {
            Text("Confirm")
                .font(.system(size: 16, weight: .medium))
                .foregroundColor(.white)
                .frame(maxWidth: .infinity)
                .frame(height: 45)
                .background(Color.blue)
                .cornerRadius(22)
        }
        .padding(.horizontal, 16)
        .padding(.bottom, 15)
    }
}

extension Phasbook {
    static var mockData: [Phasbook] = [
        Phasbook(sarcling: "选项一", storyitious: "1"),
        Phasbook(sarcling: "选项二", storyitious: "1"),
        Phasbook(sarcling: "选项三", storyitious: "1"),
        Phasbook(sarcling: "选项四", storyitious: "1"),
        Phasbook(sarcling: "选项五", storyitious: "1"),
        Phasbook(sarcling: "选项六", storyitious: "1"),
        Phasbook(sarcling: "选项七", storyitious: "1"),
        Phasbook(sarcling: "选项八", storyitious: "1"),
        Phasbook(sarcling: "选项九", storyitious: "1"),
        Phasbook(sarcling: "选项十", storyitious: "1")
    ]
}
